package cn.pzaily.orange.manage.controller;

import cn.pzaily.orange.core.utils.page.PageRequest;
import cn.pzaily.orange.core.utils.page.PageResponse;
import cn.pzaily.orange.core.utils.FileUtils;
import cn.pzaily.orange.core.utils.http.AppHttpCodeEnum;
import cn.pzaily.orange.core.utils.http.ResponseResult;
import cn.pzaily.orange.manage.model.SysUser;
import cn.pzaily.orange.manage.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @PackageName: cn.pzaily.orange.controller
 * @Description:
 * @Author: Pang
 * @Date: 2021-05-09 22:59
 */
@RestController
@RequestMapping(value = "/user")
public class SysUserController {

    @Autowired
    private SysUserService userServer;

    /**
     * 分页查询用户数据
     *
     * @param request 请求分页参数
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public ResponseResult findPage(@RequestBody PageRequest request) {
        PageResponse response = userServer.findPage(request);
        return ResponseResult.okResult(response);
    }

    /**
     * 导出excel报表
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void exportExcelUser(@RequestBody PageRequest request, HttpServletResponse response) {
        File userExcelFile = userServer.createUserExcelFile(request);
        FileUtils.downloadFile(response, userExcelFile, userExcelFile.getName());
    }

    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseResult save(@RequestBody SysUser user) {
        boolean save = userServer.save(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
